﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace Eniux.Windows.Controls.Buttons
{
    public class DropDownButton : MenuItem
    {
        static DropDownButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DropDownButton), new FrameworkPropertyMetadata(typeof(DropDownButton)));
        }

        #region Style里部件名称
        private const string PART_DownButton = "PART_DownButton";
        private const string PART_Children = "PART_Children";
        #endregion

        #region 内部变量
        internal Button P_DownButton { get; private set; }
        internal Popup P_Children { get; private set; }
        #endregion

        #region 注册属性
        #region 注册Sys_IsDropDownOpen(打开子容器)
        public bool Sys_IsDropDownOpen
        {
            get { return (bool)GetValue(Sys_IsDropDownOpenProperty); }
            set { SetValue(Sys_IsDropDownOpenProperty, value); }
        }
        public static readonly DependencyProperty Sys_IsDropDownOpenProperty =
            DependencyProperty.Register("Sys_IsDropDownOpen",
            typeof(bool),
            typeof(DropDownButton),
            new PropertyMetadata(false, OnSys_IsDropDownOpenPropertyChanged));
        private static void OnSys_IsDropDownOpenPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DropDownButton sb = (DropDownButton)d;
            if (d != null && sb.P_Children != null)
                sb.P_Children.IsOpen = (bool)e.NewValue;
        }
        #endregion

        #region 注册Sys_MaxDropDownHeight(子容器最大高度)
        public double Sys_MaxDropDownHeight
        {
            get { return (double)GetValue(Sys_MaxDropDownHeightProperty); }
            set { SetValue(Sys_MaxDropDownHeightProperty, value); }
        }
        public static readonly DependencyProperty Sys_MaxDropDownHeightProperty =
            DependencyProperty.Register("Sys_MaxDropDownHeight",
            typeof(double),
            typeof(DropDownButton),
            new PropertyMetadata(double.NaN));
        #endregion
        #endregion

        #region 重写
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            P_Children = GetTemplateChild(PART_Children) as Popup;
            P_DownButton = GetTemplateChild(PART_DownButton) as Button;
            if (P_DownButton != null)
                P_DownButton.Click += new RoutedEventHandler(P_DownButton_Click);
        }

        void P_DownButton_Click(object sender, RoutedEventArgs e)
        {
            if (P_Children != null)
            {
                if (HasItems)
                    P_Children.IsOpen = Sys_IsDropDownOpen = true;
                else
                    P_Children.IsOpen = Sys_IsDropDownOpen = false;
            }
        }
        #endregion
    }
}
